package com.dm.library.widgets.wheelview.common;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.dm.library.R;

import java.util.List;

/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  时间，地址选择弹框
 * Created by tangjian on 2016/11/22.
 */

public class WheelViewDialog extends Dialog {
    private View view;
    private CommonUseWheelView mCommonUseWheelView;
    private OnViewClickListener mOnViewClickListener;

    public void setOnViewClickListener(OnViewClickListener onViewClickListener) {
        this.mOnViewClickListener = onViewClickListener;
    }

    public WheelViewDialog(Context context) {
        super(context, R.style.customDialog);
        view = LayoutInflater.from(context).inflate(R.layout.wheel_view_dialog, null);
        mCommonUseWheelView = (CommonUseWheelView) view.findViewById(R.id.common_wheel_view);
        mCommonUseWheelView.setOnClickListener(new CommonUseWheelView.OnClickListener() {
            @Override
            public void onCancelClick() {
                dismiss();
            }

            @Override
            public void onDownClick(String firstItem, String secondItem, String thirdItem, String fourthItem) {
                dismiss();
                if (null != mOnViewClickListener) {
                    mOnViewClickListener.onCompleteClick(firstItem, secondItem, thirdItem, fourthItem);
                }
            }
        });

        view.findViewById(R.id.top_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        this.setContentView(view);
        this.setCancelable(true);
        this.setCanceledOnTouchOutside(true);
    }

    public void show(Activity activity) {
        WindowManager windowManager = activity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        if (null != getWindow()) {
            getWindow().setGravity(Gravity.BOTTOM);  //此处可以设置dialog显示的位置
            getWindow().setWindowAnimations(R.style.animBottom);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.width = display.getWidth(); //设置宽度
            getWindow().setAttributes(lp);
        }
        this.show();
    }

    public void initTimeDatas(boolean isShowHour) {
        mCommonUseWheelView.initTimeDatas(isShowHour);
        mCommonUseWheelView.setWheelViewVisibility(true, true, false);
    }
    private String tag = this.getClass().getSimpleName();
    public void setAreaDatas(List<ProvinceInfo> provinceInfos) {
        if (null != provinceInfos) {
            Log.i(tag,"setAreaDatas() -- mCommonUseWheelView:"+mCommonUseWheelView);
            mCommonUseWheelView.setAreaDatas(provinceInfos, true);
            mCommonUseWheelView.setWheelViewVisibility(true, true, false);
        } else {
            throw new IllegalArgumentException("数据集合不能为空");
        }
    }

    public void setCommonDatas(List<String> commonDatas) {
        if (null != commonDatas) {
            mCommonUseWheelView.setCommonDatas(commonDatas);
            mCommonUseWheelView.setWheelViewVisibility(false, false, false);
        } else {
            throw new IllegalArgumentException("数据集合不能为空");
        }
    }

    public interface OnViewClickListener {
        /**
         * 完成
         */
        void onCompleteClick(String first, String second, String third, String fourth);
    }
}
